<script setup lang="ts">
/**
 * 注册页面
 * @author 高桥凉介
 * @date 2024-12-24
 */
import { reactive } from "vue";
import { ElMessage } from "element-plus";
import type { RegisterForm } from "@/types";

// 定义注册表单的数据类型
const registerForm: RegisterForm = reactive({
  username: "",
  email: "",
  password: "",
  confirmPassword: "",
});

// 表单校验规则
const rules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 20, message: "用户名长度在 3 到 20 个字符", trigger: "blur" },
  ],
  email: [
    { required: true, message: "请输入邮箱地址", trigger: "blur" },
    { type: "email", message: "请输入正确的邮箱格式", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "密码长度在 6 到 20 个字符", trigger: "blur" },
  ],
  confirmPassword: [
    { required: true, message: "请再次输入密码", trigger: "blur" },
    {
      validator: (rule: any, value: string) => {
        if (value !== registerForm.password) {
          return Promise.reject("两次输入的密码不一致");
        }
        return Promise.resolve();
      },
      trigger: "blur",
    },
  ],
};

// 模拟注册函数
const handleRegister = async () => {
  // 表单校验
  const { username, email, password, confirmPassword } = registerForm;
  if (!username || !email || !password || !confirmPassword) {
    ElMessage.error("请填写完整的注册信息！");
    return;
  }

  // 模拟注册逻辑
  ElMessage.success(`注册成功，欢迎您，${registerForm.username}！`);
};
</script>

<template>
  <div class="register-page">
    <el-card shadow="always" class="card">
      <h2 class="register-title">注册</h2>
      <el-form
          :model="registerForm"
          :rules="rules"
          ref="registerFormRef"
          label-width="0"
          class="register-form"
      >
        <el-form-item prop="username">
          <div class="input-wrapper">
            <span class="input-label">用户名：</span>
            <el-input
                v-model="registerForm.username"
                placeholder="请输入用户名"
                prefix-icon="el-icon-user"
                clearable
            />
          </div>
        </el-form-item>

        <el-form-item prop="email">
          <div class="input-wrapper">
            <span class="input-label">邮箱：</span>
            <el-input
                v-model="registerForm.email"
                placeholder="请输入邮箱"
                prefix-icon="el-icon-message"
                clearable
            />
          </div>
        </el-form-item>

        <el-form-item prop="password">
          <div class="input-wrapper">
            <span class="input-label">密码：</span>
            <el-input
                v-model="registerForm.password"
                placeholder="请输入密码"
                prefix-icon="el-icon-lock"
                show-password
                clearable
            />
          </div>
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <div class="input-wrapper">
            <span class="input-label">确认密码：</span>
            <el-input
                v-model="registerForm.confirmPassword"
                placeholder="请再次输入密码"
                prefix-icon="el-icon-lock"
                show-password
                clearable
            />
          </div>
        </el-form-item>

        <el-button
            type="primary"
            class="register-button"
            @click="handleRegister"
        >
          注册
        </el-button>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
/* 页面整体样式 */
.register-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-image: url("../assets/bbgg.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 1rem;
  box-sizing: border-box;
}

.card {
  width: 400px;
  padding: 2rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 15px;
  background-color: rgba(255, 255, 255, 0.8);
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
}

.register-title {
  text-align: center;
  font-size: 2rem;
  font-weight: bold;
  color: #2c3e50;
  margin-bottom: 2rem;
}

/* 表单样式 */
.register-form {
  width: 100%;
}

.el-form-item {
  margin-bottom: 1.2rem;
}

.input-wrapper {
  display: flex;
  align-items: center;
  width: 100%;
}

.input-label {
  font-size: 1rem;
  color: #333;
  width: 100px;
  margin-right: 10px;
  line-height: 32px; /* 确保标签与输入框垂直居中 */
}

.el-input {
  flex-grow: 1;
  border-radius: 8px;
  padding: 0.5rem 1rem;
  font-size: 1rem;
}

.el-button.register-button {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 45px;
  margin-top: 1.5rem;
  font-size: 1rem;
  font-weight: bold;
  color: #ffffff;
  background-color: #74b9ff;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.el-button.register-button:hover {
  background-color: #0984e3;
}

.el-button.register-button:active {
  background-color: #0652dd;
}

/* 响应式优化 */
@media (max-width: 500px) {
  .card {
    width: 100%;
    padding: 1.5rem;
  }

  .register-title {
    font-size: 1.5rem;
  }

  .el-input,
  .el-button {
    font-size: 0.9rem;
  }
}
</style>
